/**
 * Copyright (c) 2014- 2015 YCY Group Holding Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.jkwzms.system.user.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jkwzms.common.inject.annotation.BaseComponent;
import com.jkwzms.common.service.BaseService;
import com.jkwzms.system.user.entity.UserLastOnline;
import com.jkwzms.system.user.repository.UserLastOnlineRepository;

/**
 * <p>
 * User: Jack Liang
 * <p>
 * Date: 13-2-4 下午3:01
 * <p>
 * Version:1.0.0
 */
@Service
public class UserLastOnlineService extends BaseService<UserLastOnline, Long> {

    @Autowired
    @BaseComponent
    private UserLastOnlineRepository userLastOnlineRepository;

    public UserLastOnline findByUserId(Long userId) {
        return userLastOnlineRepository.findByUserId(userId);
    }

    public void lastOnline(UserLastOnline lastOnline) {
        UserLastOnline dbLastOnline = findByUserId(lastOnline.getUserId());

        if (dbLastOnline == null) {
            dbLastOnline = lastOnline;
        } else {
            UserLastOnline.merge(lastOnline, dbLastOnline);
        }
        dbLastOnline.incLoginCount();
        dbLastOnline.incTotalOnlineTime();
        // 相对于save or update
        save(dbLastOnline);
    }
}
